package demo.codeanalyzer.processor;

import java.util.Collection;
import java.util.List;

import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.AnnotationValue;
import javax.lang.model.element.Element;
import javax.lang.model.element.Name;

import com.sun.source.util.TreePath;
import com.sun.source.util.Trees;

public class BaseCodeGenerator {

	protected PrintingVisitor visitor;

	protected Element element(Trees trees) {
		TreePath currentPath = visitor.getCurrentPath();
		Element element = trees.getElement(currentPath);
		return element;
	}

	public void setVisitor(PrintingVisitor visitor) {
		this.visitor = visitor;
	}

	protected String annotationValue(Element element, String type) {
		List<? extends AnnotationMirror> annotationMirrors = element
				.getAnnotationMirrors();
		for (AnnotationMirror mirror : annotationMirrors) {
			Name name = mirror.getAnnotationType().asElement().getSimpleName();
			if (type.equals(name.toString().toLowerCase())) {
				return anotationValue(mirror).toString();
			}
		}
		return null;
	}

	private String anotationValue(AnnotationMirror mirror) {
		Collection<? extends AnnotationValue> values = mirror
				.getElementValues().values();
		StringBuffer result = new StringBuffer();
		for (AnnotationValue value : values) {
			result.append(value.toString());
		}
		return result.toString();
	}

}
